<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>

<% 
session("redir")=""
session("ticketsaved")=0
fromdispatch=request.QueryString("FromDispatch")
'----------------- status -------------------------
set db=server.CreateObject("adodb.connection")
set rs=server.CreateObject("adodb.recordset")
		  
filepath=server.MapPath("access/backend/admin.mdb")
if fromdispatch="yes" then
	criteria="'dispatch','both'"
else
	criteria="'operator','both'"
end if
		
sql="select status_name from dispatchcolors where type='status' order by status_name"
stats=""
statusoutput=""
stats=""
acceptablestatus=""
		  
db.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath
rs.cursorlocation=3
		  
rs.open sql,db,1,2
rs.activeconnection=nothing

do until rs.eof
	statusoutput=statusoutput & "<option value='" & rs("status_name") & "'>" & rs("status_name") & "</option>" & vbcrlf
	stats=stats & rs("status_name") & "|"
	rs.movenext
loop
stats=left(stats,len(stats)-1)
rs.close

sql="select status_name from dispatchcolors where [where] in (" & criteria & ") and type='status' order by status_name"

rs.open sql,db,1,2

rs.movefirst
do until rs.eof
	acceptablestatus=acceptablestatus & rs("status_name") & "|"
	rs.movenext
loop
acceptablestatus=left(acceptablestatus,len(acceptablestatus)-1)

rs.close
db.close
'------------------- end status --------------------

'------------------- begin tickets history-----------------
FilePath=server.MapPath("access/backend/tickets.mdb")
db.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filepath
ticketNum=request.QueryString("Ticket_ID")
if ticketNum<>"" then
	sql="select * from ticket_history where Ticket_ID=" & ticketnum
	rs.open sql,db,1,2
	if not rs.eof then
		x=0
		ra=rs.getrows() '0=Note_ID, 1=Ticket_ID, 2=User, 3=When, 4=Note
		rs.close
		rowcount=ubound(ra,2)
		do until x>rowcount
			notesoutput=notesoutput & "<tr><td bgcolor='#D2FFD2' class='TableTitles'>" & x+1 & ") " & ra(2,x) & " &nbsp; " & ra(3,x) & "</td><td bgcolor='#D2FFD2' class='TableTitles'>" & ra(4,x) & "</td></tr>" & vbcrlf
		x=x+1
		loop
		
	else
		rowcount=0
		notesoutput="<tr><td>(This Ticket Has No History)</td></tr>"
	end if
	'---------------- end ticket history -----------------------
	
	'---------------- begin ticket --------------------------
	sql="select * from tickets where ticket_id=" & ticketNum
	set trs=server.CreateObject("adodb.recordset")
	'if rs.state=1 then rs.close
	trs.open sql,db,1,2
	ra=trs.getrows() '0=Ticket_ID, 1=Contract_ID, 2=Status, 3=Given_To, 4=Created_Time, 5=Completed_Time, 6=Appliance, 7=Emergency, 8=follow up
end if
	'------------------ end ticket -----------------------
	'------------------ end ASP --------------------------	
%>	
	
<HTML XMLNS="http://www.w3.org/1999/xhtml">
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1" />
<TITLE>Ticket Window</TITLE>
<LINK HREF="UniversalStyleSheet.css" REL="stylesheet" TYPE="text/css" />
<STYLE TYPE="text/css">
<!--
body {
	margin-left: 1px;
	margin-top: 1px;
	margin-right: 1px;
	margin-bottom: 1px;
}

-->
</STYLE>

<SCRIPT TYPE="TEXT/VBSCRIPT">
function FrmSubmit()

	CID=document.getElementById("Contract_ID").value
	Appl=document.getElementById("Appliance").selectedIndex
	Emerg=document.getElementById("Emergency").value
	Note=document.getElementById("Notes").value
	stat=document.getElementById("Status").selectedIndex
	msg=""
	<%
	if fromdispatch="yes" then
		%>
		distime=document.getElementById("GivenToTime").value
		<%
	end if
	%>

	set emer=document.getElementById("Emergency")
	set tf=document.getElementById("TicketForm")

	if CID="" then
		msgbox "You Have Not Selected An Account To Save The Ticket To." & vbcrlf & "If You've Already Pulled Up An Account Then Click 'New Ticket' To Associate the Contract ID With This Ticket.",vbcritical,"Operator Reminder"
 		exit function
	end if

	if stat=-1 or stat=0 then msg=msg & "- Status" & vbcrlf

	if Appl=-1 or Appl=0 then msg=msg & "- Appliance" & vbcrlf

	if Emerg<>"Yes" and Emerg<>"No" then msg=msg & "- Emergency" & vbcrlf

	if Note="" then msg=msg & "- Notes" & vbcrlf
	

	if msg <>"" then
		msgbox "Cant Save Changes. The Following Required Fields Were Blank:" & vbcrlf & vbcrlf & msg,vbcritical,"Operator Reminder"
		exit function
	end if

	tf.onsubmit =""
	<% 
	if fromdispatch="yes" then 
		%>
		document.TicketForm.action="SaveTicket.asp?FromDispatch=yes&user=<%= request.Cookies("Hsuser") %>"
		<% 
	else 
		randomize() 'this randomized number is to make the saveticket url unique so the IE bug doesnt run the code twice
		uniqueid=hex(100000000 * rnd)
		%>
		tf.action="SaveTicket.asp?uid=<%= uniqueid %>&user=<%= request.Cookies("Hsuser") %>"
		<% 
	end if 
	%>
	tf.Submit()
end function

function status_change()
	set stat=document.getElementById("status")
	set oldstat=document.getElementById("statusPH")
	
	acceptable=0
	
	accpt="<%= acceptablestatus %>"
	accptarray=split(accpt,"|")
	
	for x=0 to ubound(accptarray)
		if stat.value= accptarray(x) then acceptable=1
	next
	
	if acceptable=0 then
		msgbox "Setting this ticket to a status of '" & stat.value & "' is not allowed in this screen.",vbcritical,"User Reminder"
		stat.selectedindex=oldstat.value
		exit function
	end if
	
	oldstat.value=stat.selectedindex
	
end function
</SCRIPT>

</HEAD>

<BODY>
<FORM ID="TicketForm" NAME="TicketForm" METHOD="post" ACTION="SaveTicket.asp" onSubmit="javascript: return false;">
<TABLE WIDTH="600" BORDER="0" CELLSPACING="0" CELLPADDING="0">
 <TR>
    <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableUpperLeft.png"></TD>
    <TD WIDTH="560" HEIGHT="20" BACKGROUND="Images/TableUpperMiddle.png"></TD>
    <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableUpperRight.png"></TD>
  </TR>
  <TR>
    <TD WIDTH="20" BACKGROUND="Images/TableMiddleLeft.png"></TD>
    <TD WIDTH="460" BGCOLOR="#FFFFFF">
      <TABLE WIDTH="560" BORDER="0" CELLPADDING="0" CELLSPACING="3" BGCOLOR="#FFFFFF">
        <TR>
          <TD WIDTH="235" BGCOLOR="#D2FFD2" CLASS="SearchFormText">Contract ID
             
            <INPUT NAME="Contract_ID_Display" TYPE="text" ID="Contract_ID_Display" SIZE="15" disabled="disabled"/>
             
            <INPUT NAME="Contract_ID" TYPE="hidden" ID="Contract_ID" />
         <BR /></TD>
          <TD WIDTH="316" BGCOLOR="#D2FFD2" CLASS="SearchFormText">Ticket Number 
             
            <INPUT NAME="Ticket_ID_Display" TYPE="text" ID="Ticket_ID_Display" SIZE="15" disabled="disabled"/>
            <INPUT NAME="Ticket_ID" TYPE="hidden" ID="Ticket_ID" />
         </TD>
        </TR>
        <TR>
          <TD BGCOLOR="#D2FFD2" CLASS="SearchFormText">Appliance 
            
            <SELECT NAME="Appliance" ID="Appliance" TABINDEX="1">
              <%
			  dim fs,tf
			  set fs=server.CreateObject("scripting.filesystemobject")
			  set tf=fs.opentextfile(server.MapPath("access/backend/appliances.txt"))
			  do until tf.atendofstream
			  	appl=tf.readline
			  	response.Write("<option value='" & appl & "'>" & appl & "</option>")
				appliances=appliances & appl & "|"
			  loop
			  appliances=left(appliances,len(appliances)-1)
			  tf.close
			  set tf=nothing
			  set fs=nothing
			  %>	
			  
            </SELECT>
            </TD>
          <TD BGCOLOR="#D2FFD2" CLASS="SearchFormText"><SPAN CLASS="style10"># of Calls</SPAN>
            <INPUT NAME="CallNumber" TYPE="text" ID="CallNumber" SIZE="1" disabled="disabled"/>
            &nbsp; &nbsp;<SPAN CLASS="style10">Status</SPAN>
		  
		  
            <SELECT NAME="Status" ID="Status" TABINDEX="2" onChange="status_change()">
              <OPTION selected="selected">Choose...</OPTION>
			<%= statusoutput %>
            </SELECT>
		  
		  
		  <INPUT NAME="statusPH" TYPE="hidden" ID="statusPH">
            </TD>
        </TR>
        <TR>
          <TD BGCOLOR="#D2FFD2" CLASS="SearchFormText">Emergency: 
             
			  &nbsp;
              <INPUT NAME="Emergency" TYPE="radio" CLASS="RadioButtons" ID="Emergency" VALUE="Yes" />
              Yes  
              &nbsp; &nbsp; &nbsp;
			  <INPUT NAME="Emergency" TYPE="radio" CLASS="RadioButtons" ID="Emergency" VALUE="No" />
               
              No 
            <BR />            
	    </TD>
          <TD  bgcolor="#D2FFD2" CLASS="SearchFormText">
			<P>Given To
			  <% if fromdispatch<>"yes" then %>
			  	<INPUT NAME="GivenTo" TYPE="text" disabled="disabled" ID="GivenTo" TABINDEX="4"/>
			  <% else %>
			  	
			  	<INPUT NAME="GivenTo" TYPE="text" SIZE="20" ID="GivenTo" TABINDEX="4" />									
			  	<BR/>
			  	Dispatched Time 
			  	<INPUT NAME="GivenToTime" TYPE="text" SIZE="20" ID="GivenToTime" />
				<BR/>
				Follow Up Time
				<INPUT NAME="FollowUp" TYPE="text" SIZE="20" ID="FollowUp" />
		      <% end if %>
		    </P>
         </TD>
        </TR> 
        <TR>
          <TD COLSPAN="2" VALIGN="top" BGCOLOR="#D2FFD2" CLASS="SearchFormText">Notes 
             
            <TEXTAREA NAME="Notes" COLS="50" ROWS="5" ID="Notes"></TEXTAREA>
             </TD>
          </TR>
        <TR>
          <TD HEIGHT="10">&nbsp;</TD>
          <TD HEIGHT="10" ALIGN="right"><INPUT TYPE="image" NAME="Save" SRC="Images/SaveButton.png" ALT="save" WIDTH="60" HEIGHT="30" BORDER="0"  onclick="FrmSubmit()"/> 
		</TD>
        </TR>
		<%= notesoutput %>
		
<% if ticketnum<>"" then %>
	<SCRIPT TYPE="text/javascript">
		document.getElementById("Contract_ID_Display").value="<%= ra(1,0) %>"
		document.getElementById("Contract_ID").value="<%= ra(1,0) %>"
		document.getElementById("Ticket_ID_Display").value="<%= ra(0,0) %>"
		document.getElementById("Ticket_ID").value="<%= ra(0,0) %>"
		document.getElementById("CallNumber").value="<%= rowcount+1 %>"
		<% if request.QueryString("FromDispatch")<>"yes" then %> 
			document.getElementById("GivenTo").value="<%= ra(3,0) & " : " & ra(5,0)%>"
		<% else %>
			document.getElementById("GivenTo").value="<%= ra(3,0) %>"
			document.getElementById("GivenToTime").value="<%= ra(5,0) %>"
			document.getElementById("FollowUp").value="<%= ra(8,0) %>"
		<% end if %>
		 <% 'appliance list with selection based on chosen appliance
			x=0
			appliance=split(appliances,"|")
			do while x<=ubound(appliance)
				if ra(6,0)=appliance(x) then
					%>
					document.getElementById("Appliance").selectedIndex=<%= x %>
					<%
					exit do
				end if
				x=x+1
			loop
							
				
			'------------- set status to ticket status
			x=0
			statuses=stats
			statarray=split(statuses,"|")
			do while x<=ubound(statarray)
				if ra(2,0)=statarray(x) then
					%>
					document.getElementById("Status").selectedIndex=<%= x+1 %>
					document.getElementById("statusPH").value= "<%= x+1 %>"
					<%
					
					exit do
				end if
				x=x+1
			loop
			
			
			'------------- emergency radio buttons
			%>
			
			<%
			if ra(7,0)="Yes" then
				%>
				document.TicketForm.Emergency[0].checked=true
				<%
			end if
			
			if ra(7,0)="No" then
				%>
				document.TicketForm.Emergency[1].checked=true
				<%
			end if
					  
		%>
	</SCRIPT>
<%
end if 'the if that checks if ticketnum<>""

ContractId=request.QueryString("Contract_ID")
if ContractID<>"" then
	%>
	<SCRIPT TYPE="text/javascript">
		document.getElementById("Contract_ID_Display").value="<%= ContractId %>"
		document.getElementById("Contract_ID").value="<%= ContractId %>"
		document.getElementById("CallNumber").value=1
	</SCRIPT>

	<%
end if 'for if contractid<>""

if isobject("trs") then 
	if trs.state=1 then trs.close
end if

set trs=nothing

if rs.state=1 then rs.close
set rs=nothing

db.close
set db=nothing
%>
	</TABLE>
            
    </TD>
    <TD WIDTH="20" BACKGROUND="Images/TableMiddleRight.png"></TD>
  </TR>
  <TR>
    <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableLowerLeft.png"></TD>
    <TD WIDTH="560" HEIGHT="20" BACKGROUND="Images/TableLowerMiddle.png"></TD>
    <TD WIDTH="20" HEIGHT="20" BACKGROUND="Images/TableLowerRight.png"></TD>
  </TR>
</TABLE>
</FORM>
</BODY>
</HTML>
